package com.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;

import java.lang.reflect.Method;

public class proxyTest {
    @Test
    public void t1(){
        //创建目标类对象
        Target target=new Target();
        //创建代理类对象
        Target cglibProxy=new CglibProxy(target);
        //调用方法执行
        cglibProxy.method();
    }
    @Test
    public void t2(){
        //创建目标对象
        Target target = new Target();
        Enhancer enhancer = new Enhancer();   //创建增强器
        enhancer.setSuperclass(Target.class); //设置父类
        enhancer.setCallback(new MethodInterceptor() { //设置回调
            @Override
            public Object intercept(Object o, Method method, Object[] objects,
                                    MethodProxy methodProxy) throws Throwable {
                System.out.println("前置代码增强2....");
                Object invoke = method.invoke(target, objects);
                System.out.println("后置代码增强2....");
                return invoke;
            }
        });
        Target proxy = (Target) enhancer.create(); //创建代理对象
        proxy.method();
    }


}
